<template>
  <div id="app">
    <div class="chat-container">
      <div class="chat-header">
        <h1>咻咻</h1>
      </div>
      <!-- 聊天消息展示区域 -->
      <div class="chat-messages" ref="chatMessages">
        <div
          v-for="(message, index) in messages"
          :key="index"
          :class="message.isUser ? 'user-message' : 'bot-message'"
        >
          <!-- 用户消息时，头像在右，内容在左 -->
          <div v-if="message.isUser" class="message-bubble">
            {{ message.text }}
          </div>
          <div v-if="message.isUser" class="user-avatar"></div>
          <!-- 机器人消息时，头像在左，内容在右 -->
          <div v-if="!message.isUser" class="bot-avatar"></div>
          <div v-if="!message.isUser" class="message-bubble">
            {{ message.text }}
          </div>
        </div>
      </div>
      <!-- 输入框和发送按钮区域 -->
      <div class="input-area">
        <input
          v-model="newMessage"
          placeholder="输入你的消息"
          @keyup.enter="sendMessage"
        />
        <button @click="sendMessage">
          <i class="fa fa-paper-plane"></i> 发送
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { apiConfig } from "../config/config.js";


export default {
  name: "App",
  data() {
    return {
      // 存储聊天消息的数组
      messages: [],
      // 存储用户输入的新消息
      newMessage: "",
    };
  },
  methods: {
    async sendMessage() {
      if (this.newMessage.trim()!== "") {
        const url = `${apiConfig.baseUrl}/aiModel/test?request=${this.newMessage}`;
        console.log(url)
        try {
          const response = await axios.get(url);
          this.messages.push({ text: this.newMessage, isUser: true });
          this.messages.push({ text: response.data, isUser: false });
          // 发送消息后滚动到底部
          this.$nextTick(() => {
            this.scrollToBottom();
          });
        } catch (error) {
          console.error("请求出错:", error);
        } finally {
          // 清空输入框
          this.newMessage = "";
        }
      }
    },
    scrollToBottom() {
      const chatMessages = this.$refs.chatMessages;
      chatMessages.scrollTop = chatMessages.scrollHeight;
    },
  },
  mounted() {
    // 初始加载时滚动到底部
    this.scrollToBottom();
  },
};
</script>

<style scoped>
/* 去除默认的内外边距 */
html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

#app {
  height: 100%;
}

/* 整体聊天容器样式 */
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

/* 聊天头部样式 */
.chat-header {
  background-color: #007bff;
  color: white;
  padding: 15px;
  text-align: center;
  flex-shrink: 0;
}

.chat-header h1 {
  margin: 0;
  font-size: 20px;
}

/* 聊天消息展示区域样式 */
.chat-messages {
  flex-grow: 1;
  border: none;
  padding: 15px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  background-color: #f9f9f9;
}

/* 用户消息样式 */
.user-message {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
  align-items: flex-end;
}

/* 机器人消息样式 */
.bot-message {
  display: flex;
  justify-content: flex-start;
  margin-bottom: 10px;
  align-items: flex-end;
}

/* 用户头像样式 */
.user-avatar {
  width: 40px;
  height: 40px;
  background-image: url('../icon/human.jpg'); /* 替换为用户头像图片路径 */
  background-size: cover;
  border-radius: 50%;
  margin-left: 10px;
}

/* 机器人头像样式 */
.bot-avatar {
  width: 40px;
  height: 40px;
  background-image: url('../icon/robot.jpg'); /* 替换为机器人头像图片路径 */
  background-size: cover;
  border-radius: 50%;
  margin-right: 10px;
}

/* 消息气泡样式 */
.message-bubble {
  padding: 10px 15px;
  border-radius: 15px;
  max-width: 70%;
  word-wrap: break-word;
  position: relative;
}

.user-message .message-bubble {
  background-color: #007bff;
  color: white;
  border-bottom-right-radius: 0;
}

.bot-message .message-bubble {
  background-color: #e5e5ea;
  color: black;
  border-bottom-left-radius: 0;
}

/* 输入框和发送按钮区域样式 */
.input-area {
  display: flex;
  padding: 15px;
  background-color: white;
  border-top: 1px solid #eee;
  flex-shrink: 0;
}

.input-area input {
  flex: 1;
  padding: 10px;
  margin-right: 10px;
  border: 1px solid #ccc;
  border-radius: 20px;
  outline: none;
}

.input-area button {
  padding: 10px 20px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.3s ease;
}

.input-area button:hover {
  background-color: #0056b3;
}

/* 图标样式 */
.fa-paper-plane {
  margin-right: 5px;
}
</style>